<template>
	<view class="body">
		<view class="relic-body-content">
			<view class="relic-item" v-for="(item, i) in relicList" 
			:key="i" >
				<my-relic :relic="item" :likeIds="likeIds" :isClicked="isClicked" @praiseClick="praiseClickHandler"></my-relic>
			</view>
		</view>
		
		
	</view>
</template>

<script>
 	export default {
		data() {
			return {
				//跳转的文物类型
				relicType: '',
				//跳转的文物列表
				relicList: [],
				//当前用户
				visitor: {},
				//用户是否点赞
				isPraise: false,
				//用户点赞的id数组
				likeIds: [],
				//节流阀
				isClicked: false,
				//定时器
				timer: 0
			}
		},
		methods: {
			setRelicType(type) {
				this.relicType = type
			},
			async setRelicList() {
				const {data: res} = await uni.$http.get('/exhibits/list/' + this.relicType)
				// console.log("数据：",res)
				if(res.msg !== 'success') return uni.$showMsg('数据加载失败')
				
				this.relicList = res.list
			},
			async setLikeIds() {
				this.visitor = JSON.parse(uni.getStorageSync('personInfo') || '{}')
				if(JSON.stringify(this.visitor) === '{}') {
					return 
				}
				
				const {data: result} = await uni.$http.get('/visitor/likeExhibitIds/' + this.visitor.visitorIdCard)
				const IdsStr = result.likeIds
			
				if(!IdsStr) {
					this.likeIds = []
					return
				}
				const likeIds = IdsStr.split(',')
				this.likeIds = likeIds
			},
			async praiseClickHandler(exhibit) {
				
				// let timer
				if (this.timer) {
					clearTimeout(this.timer)
				}
				this.timer = setTimeout(async () => {
					// console.log("执行了！！！")
					const visitor = JSON.parse(uni.getStorageSync('personInfo') || '{}')
					if(JSON.stringify(this.visitor) === '{}') {
						// console.log('visitor: ', visitor)
						uni.showToast({
							icon: 'none',
							title: '请在点赞前先去我的页面，填写个人实名信息'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/subpkg/person/person?type=redir'
							})
						},2000)
						
						return
					}
					
					
					const isPraise = this.checkPraise(exhibit)
					// console.log("点赞状态：", isPraise)
					// 判断此时的点击状态
					var update = 0
					if(isPraise) {
						update = -1
						
						//更新本地个人喜欢文物id列表
						// this.likeIds = this.likeIds.filter(x => x != exhibit.exhibitId)
						let visitorUpdate = {
							visitorIdCard: this.visitor.visitorIdCard,
							likeExhibitIds: this.likeIds.filter(x => x != exhibit.exhibitId).join(',')
						}
						const {data: res2} = await uni.$http.post('/visitor/updateIds', visitorUpdate)
						if(res2.msg !== 'success') return uni.$showMsg('更新个人喜爱的文物列表失败！')
						this.setLikeIds()
						
					}else {
						update = 1
						this.likeIds = [...this.likeIds,exhibit.exhibitId]
						
						let visitorUpdate = {
							visitorIdCard: this.visitor.visitorIdCard,
							likeExhibitIds: this.likeIds.join(',')
						}
						
						const {data: res2} = await uni.$http.post('/visitor/updateIds', visitorUpdate)
						if(res2.msg !== 'success') return uni.$showMsg('更新个人喜爱的文物列表失败！')
						this.setLikeIds()
					}
					
					//修改点赞总数
					const newExhibit = {
						exhibitId: exhibit.exhibitId,
						exhibitNo: exhibit.exhibitNo,
						praises: exhibit.praises + update,
					}
					
					if(exhibit.praises + update < 0) return 
					
					//给后端发送请求改变数据
					const {data: res} = await uni.$http.post('/exhibits/update/praise', newExhibit)
					if(res.msg !== 'success') return uni.$showMsg('修改文物点赞总数失败')
					
					//变换本地点赞个数
					this.setRelicList()
					
					if(this.checkPraise(exhibit) === false) return uni.$showMsg('取消点赞', 2000)
					else return uni.$showMsg('点赞成功', 2000)
						
						
					
					// uni.$showMsg("请勿频繁点击！")
					this.setIsClicked()
				}, 1000)
				
				
				// const visitor = JSON.parse(uni.getStorageSync('personInfo') || '{}')
				// if(JSON.stringify(this.visitor) === '{}') {
				// 	return uni.showToast({
				// 		icon: 'none',
				// 		title: '请在点赞前先去我的页面，填写个人实名信息'
				// 	})
				// }
				
				// if(!this.isClicked) {
				// 	this.isClicked = true
				// 	const isPraise = this.checkPraise(exhibit)
				// 	// console.log("点赞状态：", isPraise)
				// 	// 判断此时的点击状态
				// 	var update = 0
				// 	if(isPraise) {
				// 		update = -1
						
				// 		//更新本地个人喜欢文物id列表
				// 		// this.likeIds = this.likeIds.filter(x => x != exhibit.exhibitId)
				// 		let visitorUpdate = {
				// 			visitorIdCard: this.visitor.visitorIdCard,
				// 			likeExhibitIds: this.likeIds.filter(x => x != exhibit.exhibitId).join(',')
				// 		}
				// 		const {data: res2} = await uni.$http.post('/visitor/updateIds', visitorUpdate)
				// 		if(res2.msg !== 'success') return uni.$showMsg('更新个人喜爱的文物列表失败！')
				// 		this.setLikeIds()
						
				// 	}else {
				// 		update = 1
				// 		this.likeIds = [...this.likeIds,exhibit.exhibitId]
						
				// 		let visitorUpdate = {
				// 			visitorIdCard: this.visitor.visitorIdCard,
				// 			likeExhibitIds: this.likeIds.join(',')
				// 		}
						
				// 		const {data: res2} = await uni.$http.post('/visitor/updateIds', visitorUpdate)
				// 		if(res2.msg !== 'success') return uni.$showMsg('更新个人喜爱的文物列表失败！')
				// 		this.setLikeIds()
				// 	}
					
				// 	//修改点赞总数
				// 	const newExhibit = {
				// 		exhibitId: exhibit.exhibitId,
				// 		exhibitNo: exhibit.exhibitNo,
				// 		praises: exhibit.praises + update,
				// 	}
					
				// 	if(exhibit.praises + update < 0) return 
					
				// 	//给后端发送请求改变数据
				// 	const {data: res} = await uni.$http.post('/exhibits/update/praise', newExhibit)
				// 	if(res.msg !== 'success') return uni.$showMsg('修改文物点赞总数失败')
					
				// 	//变换本地点赞个数
				// 	this.setRelicList()
					
				// 	if(this.checkPraise(exhibit) === false) return uni.$showMsg('取消点赞', 2000)
				// 	else return uni.$showMsg('点赞成功', 2000)
					
					
				// }
				// uni.$showMsg("请勿频繁点击！")
				// this.setIsClicked()
				
			},
			//检查该文物是否被用户点赞
			checkPraise(item) {
				
				if(this.likeIds === []) {
					return false
				}
				else {
					let flag = 0
					this.likeIds.forEach(x => {
						if(x === item.exhibitId) {
							flag = 1
						}
					})
					if(flag === 1) return true
					else return false
				}
			},
			// setIsClicked() {
			// 	this.timer = setInterval(() => {
			// 		this.isClicked = false
			// 	},1000)
			// }

		},
		onLoad(options) {
			//设置跳转文物类型
			this.setRelicType(options.type)
			//设置文物列表
			this.setRelicList()
			//设置用户点赞的文物id数组
			this.setLikeIds()
		},
		beforeDestroy() {
			clearInterval(this.timer)
		}
	}
</script>

<style lang="scss">
.body {	
	width: 370px;
	//文物的主盒子
	.relic-body-content {
		display: flex;
		justify-content: space-around;
		margin: 10px;
		flex-wrap: wrap;
	
	}
}
</style>
